package thread.wang_bao_ling.c14;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 演示ReentrantLock 中断队列中等待的线程
 * @Auther: luobiao
 * @Date: 2020/7/5 09:26
 * @Description:
 */
public class DemoReentrantLockInterrupted {
    public static ReentrantLock reentrantLock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            System.out.println("t1 run ...");
            try{
                reentrantLock.lock();
                System.out.println("t1 get lock");
                try {
                    Thread.currentThread().sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }finally {
                reentrantLock.unlock();
                System.out.println("t1 unlock");
            }
            System.out.println("t1 run end ....");
        },"t1");
        t1.start();

        Thread.sleep(100);

        Thread t2=new Thread(() -> {
            System.out.println("t2 run ...");
            try {
                reentrantLock.lock();
                System.out.println("t2 get lock");
            }finally {
                reentrantLock.unlock();
                System.out.println("t2 unlock");
            }
            System.out.println("t2 run end ....");
        }, "t2");
        t2.start();


        System.out.println("t2 interrupt begin");
        t2.interrupt();
        System.out.println("t2 interrupt end");


    }
}
